﻿using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace BindTryParseMVC.Models
{
    public class Locale : CultureInfo, IParsable<Locale>
    {
        public Locale(string culture) : base(culture)
        {
        }

        public static Locale Parse(string s, IFormatProvider? provider)
        {
            if(!TryParse(s,provider, out var result))
            {
                throw new ArgumentException($"Cannot parse '{s}' to Locale.");
            }
            return result;
        }

        public static bool TryParse([NotNullWhen(true)] string? value, IFormatProvider? provider, [MaybeNullWhen(false)] out Locale locale)
        {
            if(value is null)
            {
                locale = new Locale(CurrentCulture.Name);
                return false;
            }
            try
            {
                locale = new Locale(value);
                return true;
            }
            catch (CultureNotFoundException)
            {
                locale = new Locale(CurrentCulture.Name);
                return false;
            }
        }
    }
}
